const browser = require('../browser');
const { logger } = require('../../utils/logger');

class PlaywrightScreenshot {
  static async takeScreenshot(url, options = {}) {
    return browser.runWithPlaywright(url, options, async (page) => {
      const screenshotOptions = {
        type: options.format || 'png',
        fullPage: options.fullPage || false
      };
      
      if (screenshotOptions.type === 'jpeg' || screenshotOptions.type === 'jpg') {
        screenshotOptions.quality = options.quality || 80;
      }
      
      const screenshot = await page.screenshot(screenshotOptions);
      
      logger.info(`Playwright截图成功: ${url}`);
      
      return {
        screenshot: `data:image/${screenshotOptions.type};base64,${screenshot.toString('base64')}`,
        url,
        timestamp: new Date().toISOString()
      };
    });
  }
}

module.exports = PlaywrightScreenshot;
